import java.awt.Color;
import java.awt.Graphics;
import java.util.Map;

import com.speljohan.rsbot.bot.Bot;
import com.speljohan.rsbot.event.events.ServerMessageEvent;
import com.speljohan.rsbot.event.listeners.PaintListener;
import com.speljohan.rsbot.event.listeners.ServerMessageListener;
import com.speljohan.rsbot.script.Script;
import com.speljohan.rsbot.script.Skills;
import com.speljohan.rsbot.script.wrappers.RSInterface;
import com.speljohan.rsbot.script.wrappers.RSNPC;
import com.speljohan.rsbot.script.wrappers.RSTile;

public class RogueCooker extends Script implements ServerMessageListener,
		PaintListener {

	private final long startTime = System.currentTimeMillis();
	private int foodID, busy = 0, cooked, burned;
	private final int wait1 = 200, wait2 = 400, bankID = 2271;
	private final RSTile fireTile = new RSTile(3043, 4973);
	private boolean isCooking = false;

	@Override
	public String getAuthor() {
		return "Fusion89k";
	}

	@Override
	public String getName() {
		return "Rogue Den Cooker";
	}

	@Override
	public String getScriptCategory() {
		return "Cooking";
	}

	@Override
	public String getScriptDescription() {
		return "<html>\n" + "<head></head>\n" + "<body>\n" + "<center>"
				+ "<h2>" + getName() + "</h2>" + "</center>\n"
				+ "<b>Author:</b> " + getAuthor() + "<br />"
				+ "<b>Version:</b> " + getVersion() + "<br><br>"
				+ "Food ID: <input type='text' size='5' name='id'>"
				+ "</body>\n" + "</html>";
	}

	@Override
	public int loop() {
		if (getMyPlayer().isMoving()) {
			return random(wait1, wait2);
		}
		if (getMyPlayer().isInCombat()) {
			walkTileMM(new RSTile(fireTile.getX(), fireTile.getY() + 7));
			return random(wait1, wait2);
		} else {
			if (distanceTo(fireTile) > 3) {
				walkTileMM(fireTile);
				return random(wait1, wait2);
			}
		}
		if (isCooking && getMyPlayer().getAnimation() == -1) {
			busy++;
			if (busy > 3) {
				isCooking = false;
			}
			return random(wait1, wait2);
		} else {
			busy = 0;
		}
		if (!inventoryContains(foodID)) {
			busy = 5;
			isCooking = false;
			if (!bank.getInterface().isValid()) {
				final RSNPC booth = getNearestNPCByID(bankID);
				if (booth == null) {
					return random(wait1, wait2);
				}
				if (!tileOnScreen(booth.getLocation())) {
					walkTileMM(randomTile(booth.getLocation(), 1));
					return random(wait1, wait2);
				}
				atNPC(booth, "Bank");
			} else {
				bank.depositAll();
				clickMouse(random(38, 73), random(92, 123), false);
				atMenu("but");
				bank.close();
			}
			return random(wait1, wait2);
		} else {
			if (isCooking) {
				busy = 0;
				return random(wait1, wait2);
			}
			if (!RSInterface.getInterface(307).isValid()) {
				// if (getSelectedItemID() < 0)
				atInventoryItem(foodID, "Use");
				atTile(fireTile, "fire");
				return random(wait1, wait2);
			} else {
				clickMouse(random(80, 439), random(379, 457), false);
				wait(random(100, 300));
				atMenu("All");
				isCooking = true;
				return random(wait1, wait2);
			}
		}
	}

	@Override
	public void onFinish() {
		Bot.getEventManager().removeListener(PaintListener.class, this);
	}

	public void onRepaint(final Graphics g) {
		if (isLoggedIn()) {
			long millis = System.currentTimeMillis() - startTime;
			final long hours = millis / (1000 * 60 * 60);
			millis -= hours * 1000 * 60 * 60;
			final long minutes = millis / (1000 * 60);
			millis -= minutes * 1000 * 60;
			final long seconds = millis / 1000;
			final int topX = 515 - 173, topY = 337 - 95, x = topX + 5;
			int y = topY + 3;
			g.setColor(new Color(0, 50, 100, 100));
			g.fill3DRect(topX, topY, 515 - topX, 337 - topY, true);
			g.setColor(Color.white);
			g.drawString("Runtime: " + hours + "h " + minutes + "min "
					+ seconds + "sec.", x, y += 15);
			g.drawString("Cooked: " + Integer.toString(cooked), x, y += 15);
			g.drawString("Burnt: " + Integer.toString(burned), x, y += 15);
			final int index = Skills.getStatIndex("cooking");
			g.drawString("EXP til level: " + skills.getXPToNextLevel(index), x,
					y += 15);
			g.drawString("Current EXP: " + skills.getCurrentSkillExp(index), x,
					y += 15);
		}
	}

	@Override
	public boolean onStart(final Map<String, String> args) {
		try {
			foodID = Integer.parseInt(args.get("id"));
		} catch (final Exception e) {
			log(e.getMessage());
			return false;
		}
		return true;
	}

	private RSTile randomTile(final RSTile location, final int rand) {
		final int x = location.getX(), y = location.getY();
		return new RSTile(random(x - rand, x + rand),
				random(y - rand, y + rand));
	}

	public void serverMessageRecieved(final ServerMessageEvent e) {
		final String word = e.getMessage().toLowerCase();
		if (word.contains("advanced")) {
			isCooking = false;
		} else if (word.contains("cook")) {
			cooked++;
		} else if (word.contains("burn")) {
			burned++;
		}
	}

}
